/*
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-09-07 10:48:50
 * @LastEditors: 颜礼田 2564217790@qq.com
 * @LastEditTime: 2024-09-07 10:54:45
 * @FilePath: /webServer/src/middleware/jwt.middleware.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Config, Middleware } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { PassportMiddleware } from '@midwayjs/passport';
import { JwtStrategy } from '../strategy/jwt.strategy';
import { AuthenticateOptions } from 'passport';
import { isNotEmpty } from '../utils/isHelper';

@Middleware()
export class JwtPassportMiddleware extends PassportMiddleware(JwtStrategy) {
  /**
   *从配置中获取不需要验证的路由
   */
  @Config('iRoute')
  iRoute: Array<string>;

  getAuthenticateOptions(): Promise<AuthenticateOptions> | AuthenticateOptions {
    return {};
  }
  /**
   * 当前path是否可忽略
   * @param ctx
   * @returns
   */
  ignore(ctx: Context): boolean {
    const path = ctx.path;
    if (!isNotEmpty(this.iRoute)) return true;
    return this.iRoute.includes(path) || /\/public\//gi.test(path);
  }
}
